<script setup>
import { ref,onMounted } from 'vue'
import { queryWarehousingEntry,addWarehousingEntry,queryWarehousingEntryById,updateWarehousingEntry,deleteWarehousingEntry } from '../../api/warehousingentry'
import { queryWarehouseAll } from '../../api/warehouse'
import { queryItemList } from '../../api/item'
import { getSupplierAllList } from '../../api/supplier'
import { ElMessage } from 'element-plus'

//定义子对话框的数据
const selectDialogShow = ref(false)
const selectForm = ref({ skuName: '',spuCode: '' })
const selectableList = ref([])
//查询子表数据
const querySelectable = async () => { 
  const result = await queryItemList(pageSelect.value,pageSizeSelect.value,selectForm.value.skuName,selectForm.value.spuCode)
  selectableList.value = result.data.list
  totalSelect.value = result.data.totalSelect
}
//清空子表表单数据
const clearSelectable = () => { 
  selectForm.value = { skuName: '',spuCode: '' }
  querySelectable()
}
// 删除明细项
const deleteItem = (index) => {
  dialogData.value.detailsList.splice(index, 1)
}
//子表中添加商品的数据
const addSelectedItems = () => { 
  selectedItems.value.forEach(item => { 
      dialogData.value.detailsList.push({
        id: item.id,
        spuCode: item.spuCode,
        skuName: item.skuName,
        supplierId: '',
        count: '',
        price: ''
  })
  })
  // 关闭选择对话框
  selectDialogShow.value = false
  // 清空选中项
  selectedItems.value = []
}

//定义弹出框数据
const title = ref(null)
const dialogShow = ref(false)
const dialogData = ref({warehouseId:'',consignorId:'',batchNumber:'',predictedArrivalTime:'',detailsList:[]})

//供应商的数据
const supplierData = ref([])
const supplierAllList = async ()=>{ 
  const result = await getSupplierAllList()
  supplierData.value = result.data
}

//仓库的数据集合，有id和name两个属性
const warehouseList = ref([])
const getWarehouseAll =async () =>{
  const result = await queryWarehouseAll()
  warehouseList.value = result.data
}

//定义表格数据
const warehousingEntryList = ref([])
//定义表单数据
const warehousingEntryFrom = ref({batchNumber:''})

//分页条件查询
const query = async () =>{
  const result = await queryWarehousingEntry(page.value,pageSize.value,warehousingEntryFrom.value.batchNumber)
  warehousingEntryList.value = result.data.rows
  total.value = result.data.total
}

//定义清空表单的方法
const clear = () => { 
  warehousingEntryFrom.value = {batchNumber:''}
  query()
}

//定义分页的变量
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
//定义分页的点击事件
const handleSizeChange = (val) => {
  page.value = 1
  query()
}
const handleCurrentChange = (val) => {
  query()
}

//定义子表分页数据
const pageSelect = ref(1)
const pageSizeSelect = ref(10)
const totalSelect = ref(0)
const handleSizeSelectChange = (val) => {
  pageSelect.value = 1
  querySelectable()
}
const handleCurrentSelectChange = (val) => {
  querySelectable()
}
//定义商品集合
const selectedItems  = ref([])
//获取选中商品的id集合
const handleSelectionChange = (value) => {
  selectedItems.value = value
}


//挂载
onMounted(() => {
  query()
  getWarehouseAll()
  querySelectable()
  supplierAllList()
})

//添加的点击事件
const saveWarehousingEntryData = () => { 
  title.value = '新增到货通知'
  dialogShow.value = true
  //清空数据
  dialogData.value = {warehouseId:'',consignorId:'',batchNumber:'',predictedArrivalTime:'',detailsList:[]}
}

//保存的点击事件
const saveWarehousingEntryInfo = async () => { 
  let result = null
  if(dialogData.value.id){
    result = await updateWarehousingEntry(dialogData.value)
  }else{
    result = await addWarehousingEntry(dialogData.value)
  }
  if(result.code){
    ElMessage.success('操作成功！')
    dialogShow.value = false
    query()
  }else{
    ElMessage.error('操作失败！请重新操作')
  }
}

//回显数据
const edit = async (id)=>{
  title.value = '修改到货通知'
  dialogShow.value = true
  const result = await queryWarehousingEntryById(id) 
  dialogData.value = result.data
}

// 删除
const deleteById = async (id)=>{
  const result = await deleteWarehousingEntry(id) 
  if(result.code){
    ElMessage.success('操作成功！')
    query()
  }else{
    ElMessage.error('操作失败！请重新操作')
  }
}
</script>
<template>
<div><h4>到货通知</h4></div>
<div>
  <el-form :inline="true" :model="warehousingEntryFrom" class="demo-form-inline">
    <el-form-item>
      <el-button type="primary" @click="saveWarehousingEntryData" plain><el-icon><Plus /></el-icon></el-button>
    </el-form-item>
    <el-form-item>
      <el-input v-model="warehousingEntryFrom.batchNumber" placeholder="请输入批次号" style="width: 200px;"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="query" plain>查询</el-button>
      <el-button type="default" @click="clear" plain>清空</el-button>
    </el-form-item>
  </el-form>
</div>

<!-- table表格 -->
<div>
  <el-table :data="warehousingEntryList" border style="width: 100%" >
    <el-table-column fixed type="index" label="序号" align="center"  width="80"/>
    <el-table-column prop="consignorName" label="货主名称" align="center"  width="200"></el-table-column>
    <el-table-column prop="batchNumber" label="批次号" align="center" width="120"/>
    <el-table-column prop="totalCount" label="批次商品数量" align="center" width="200"/>
    <el-table-column prop="predictedArrivalTime" label="预测到达时间" align="center" width="150"/>
    <el-table-column prop="warehouseName" label="仓库名称" align="center" width="200"/>
    <el-table-column prop="createTime" label="创建时间" align="center" width="200"/>
    <el-table-column fixed="right" label="操作" align="center" width="120">
      <template #default="scope">
        <el-button type="success" size="small" @click="edit(scope.row.id)" plain><el-icon><EditPen /></el-icon></el-button>
        <el-button type="danger" size="small" @click="deleteById(scope.row.id)" plain><el-icon><Delete /></el-icon></el-button>
      </template>
    </el-table-column>
  </el-table>
</div>

<!-- 分页 -->
<div>
  <el-pagination
  v-model:current-page="page"
  v-model:page-size="pageSize"
  :page-sizes="[5, 10, 15, 20]"
  layout="total, sizes, prev, pager, next, jumper"
  :total="total"
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"/>
</div>

<!-- 弹出框 -->
<div>
  <el-dialog v-model="dialogShow" :title="title" width="70%" align-center>
    <el-form :model="dialogData">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item>
            <el-select v-model="dialogData.warehouseId" placeholder="请选择仓库" style="width: 200px;">
              <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-input v-model="dialogData.consignorId" placeholder="请输入货主名称" style="width: 200px;"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-input v-model="dialogData.batchNumber" placeholder="批次号" style="width: 200px;"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-date-picker v-model="dialogData.predictedArrivalTime" type="date" placeholder="预测到达时间" value-format="YYYY-MM-DD" style="width: 2000px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="selectDialogShow = true"><el-icon><Plus /></el-icon></el-button>
      </el-form-item>
      <el-row :gutter="10" v-for="(item,index) in dialogData.detailsList">
      <el-col :span="5">
        <el-form-item :label-width="formLabelWidth">
          <el-input v-model="item.skuName" placeholder="商品名称"/>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item :label-width="formLabelWidth">
          <el-input v-model="item.spuCode" placeholder="商品编码"/>
        </el-form-item>
      </el-col>
      <el-col :span="5">
        <el-form-item>
          <el-select v-model="item.supplierId" placeholder="供应商名称" style="width: 200px;">
            <el-option v-for="item in supplierData" :key="item.id" :label="item.supplierName" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item :label-width="formLabelWidth">
          <el-input v-model="item.count" placeholder="商品数量"/>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item :label-width="formLabelWidth">
          <el-input v-model="item.price" placeholder="商品价格"/>
        </el-form-item>
      </el-col>
      <el-col :span="2"><el-button type="danger" @click="deleteItem(index)" plain>删除</el-button></el-col>
    </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogShow = false" type="default" plain>取消</el-button>
      <el-button type="primary" @click="saveWarehousingEntryInfo" plain>确定</el-button>
    </template>
  </el-dialog>
</div>


<!-- 选择商品的弹窗 -->
<el-dialog v-model="selectDialogShow" title="选择商品" width="70%" align-center>
  <el-form :inline="true" @submit.prevent>
    <el-form-item>
      <el-input v-model="selectForm.skuName" placeholder="商品名称" style="width: 200px;" />
    </el-form-item>
    <el-form-item>
      <el-input v-model="selectForm.spuCode" placeholder="商品编码" style="width: 200px;" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="querySelectable" plain>查询</el-button>
      <el-button type="default" @click="clearSelectable" plain>清空</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="selectableList" @selection-change="handleSelectionChange" style="margin-top: 10px;" border height="300" >
    <el-table-column type="selection" width="55" />
    <el-table-column fixed type="index" label="序号" align="center" />
    <el-table-column prop="spuCode" label="商品编号" align="center" />
    <el-table-column prop="skuName" label="商品名称" align="center" />
    <el-table-column prop="skuPrice" label="商品价格" align="center" />
    <el-table-column prop="supplierName" label="供应商名称" align="center" />
    <el-table-column prop="brandName" label="品牌" align="center" />
  </el-table>

    <!-- 分页 -->
  <div>
    <el-pagination
    v-model:current-page="pageSelect"
    v-model:page-size="pageSizeSelect"
    :page-sizes="[5, 10, 15, 20]"
    layout="total, sizes, prev, pager, next, jumper"
    :total="totalSelect"
    @size-change="handleSizeSelectChange"
    @current-change="handleCurrentSelectChange"/>
  </div>
  <template #footer>
    <el-button @click="selectDialogShow = false">取消</el-button>
    <el-button type="primary" @click="addSelectedItems" plain>添加</el-button>
  </template>
</el-dialog>
</template>
<style scoped>
</style>